package com.jnu.tijian.handler;

import com.jnu.tijian.dto.ResponseObject;
import com.jnu.tijian.exception.TijianAPPException;
import com.jnu.tijian.exception.TijianAPPExceptionEnum;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice   // 全局异常处理器
public class GlobalExceptionHandler {
    // 只处理业务异常
    @ExceptionHandler(TijianAPPException.class)
    @ResponseBody   // 返回json数据给前端
    public ResponseObject HandleTijianAPPException(TijianAPPException e) {
        e.printStackTrace();
        System.out.println("异常信息："+e.getStackTrace());
        return new ResponseObject(e.getCode(), e.getMessage());
    }

    // 处理所有异常
    @ExceptionHandler(Exception.class)
    @ResponseBody   // 返回json数据给前端
    public ResponseObject HandleAllException(Exception e) {
        e.printStackTrace();
        System.out.println("异常信息："+e.getStackTrace()+",message:"+e.getMessage());
        return new ResponseObject(TijianAPPExceptionEnum.SYSTEM_ERROR.getCode(),TijianAPPExceptionEnum.SYSTEM_ERROR.getMessage());
    }
}
